<?php 
    $demots = View::get('demots');
    $demotCount = View::get('demotCount');
    $pager = View::get('pager');
    $demotPerPage = View::get('demotPerPage');
?>

    <?php foreach ($demots as $d): ?>
    <div id="demotPerPage" class="d-none"><?php echo View::get('demotPerPage') ?></div>
    <div id="demotCount" class="d-none"><?php echo View::get('demotCount') ?></div>

    <div class="demot-holder">
        <div class="demot-top"></div>
        <div class="demot-container">
            <div class="demot">
                <div class="title-box">
                    <div class="title">
                        <a href="<?php echo View::getRoot()?>/demot/pokaz/<?php echo $d['id'] ?>"><?php echo htmlspecialchars($d['title']); ?></a>
                    </div>
                    <div class="note">
                        <div class="dib" style="width:48%;">Dodane: <?php echo $d['date']; ?> przez <a href="#"><?php echo UserModel::getUserName(intval($d['id_user'])); ?></a></div>
                        <div class="dib note-right" style="width:48%; text-align: right">                   
                            <a href="#">skomentuj</a> |
                            <a href="<?php echo View::getRoot()?>/demot/pokaz/<?php echo $d['id'] ?>" title="<?php echo htmlspecialchars($d['title']); ?>" class="add-to-fav">ulubione</a> |
                            <a href="#">podziel się</a>
                        </div>
                    </div>
                </div>               
                <div class="image" >                    
                    <?php if (($d['id_type'] == DStatus::TYPE_IMAGE) && !intval($d['for_adults'])): ?>
                        <?php if (file_exists("./bank/demot/normal/".$d['id'].".jpg")): ?>
                            <a href="<?php echo View::getRoot()?>/demot/pokaz/<?php echo $d['id'] ?>"><img class="demot-image" src="<?php echo View::getRoot()?>/bank/demot/normal/<?php echo $d['id'] ?>.jpg"/></a>
                        <?php else: ?>
                            <a href="<?php echo View::getRoot()?>/demot/pokaz/<?php echo $d['id'] ?>"><img class="demot-image" src="<?php echo View::getRoot()?>/bank/demot/def.jpg"/></a>
                        <?php endif; ?>
                    <?php elseif (($d['id_type'] == DStatus::TYPE_MOVIE) && !$d['for_adults']): ?>
                            <?php echo System::parse_youtube_url($d['movie'], 'embed', 640, 480); ?>
                    <?php elseif (intval($d['for_adults']) == 1): ?>
                            <a href="<?php echo View::getRoot()?>/demot/pokaz/<?php echo $d['id'] ?>"><img class="demot-image" src="<?php echo View::getRoot()?>/bank/demot/def_adults.jpg"/></a>                                 
                    <?php else: ?>
                            <a href="<?php echo View::getRoot()?>/demot/pokaz/<?php echo $d['id'] ?>"><img class="demot-image" src="<?php echo View::getRoot()?>/bank/demot/def.jpg"/></a>
                    <?php endif; ?>

                    <div class="rank-component">

                        <div class="text">głosowanie:</div>
                        <a class="change" href="<?php echo View::getRoot()?>/demot/rank/<?php echo $d['id'] ?>/plus/<?php echo $pager->Page();?>">
                            <img src="<?php echo View::getPath()?>images/dobre-btn.jpg" alt="zabij_pl_rank">
                        </a>
                        <a class="change" href="<?php echo View::getRoot()?>/demot/rank/<?php echo $d['id'] ?>/minus/<?php echo $pager->Page();?>" style="margin-left:5px">
                            <img src="<?php echo View::getPath()?>images/slabe-btn.jpg" alt="zabij_pl_rank">
                        </a>
                        <div class="progress-bar">
                            <div class="orange-bar" title="<?php echo intval($d['rank_plus']); ?>"></div>
                            <div class="gray-bar" title="<?php echo intval($d['rank_minus']); ?>"></div>
                        </div>
                        <div class="progress-text">
                            głosów: <?php echo intval($d['rank_plus'])+intval($d['rank_minus']); ?> (<?php echo intval($d['rank_plus']); ?>/<?php echo intval($d['rank_minus']); ?>) 
                        </div>
                        <div class="fb-like fb-like-demot" data-href="http://tarni.pl/server/demot/pokaz/<?php echo $d['id'] ?>" data-send="false" data-layout="box_count" data-width="450" data-show-faces="false"></div>
                    </div>      

                </div>
            </div>
   </div>
<div class="demot-bottom"></div>
 </div>
<?php endforeach; ?>


<div class="clear"></div>
<?php if ($demotCount > $demotPerPage): ?>
<div style="text-align: center;">
    <div  class="dib pager" style="padding: 5px; position: relative;">
        <?php echo $pager->Render(true); ?>
        <br/>
        <div class="progress-bar" title="<?php echo View::get('demotPerPage') ?>">           
            <div class="bar"></div>
        </div>
    </div>
</div>
<?php elseif($demotCount == 0): ?>
<div class="" style="text-align: center;">Nie znaleziono wyników.</div>
<?php endif;?>

<script type="text/javascript">
$(document).ready(function()
{   
  
    function calculatePager($bar)
    {
        $showElement = parseInt($('#demotPerPage'));
        $countElement = parseInt($('#demotCount')); //114; //parseInt($bar.parent().attr('title'));
               
        $maxWidth = $bar.parent().width();
  
        $width = parseInt(($maxWidth*$showElement/$countElement));
             
        
        $bar.width( $width );
      
    }
    
    
    
    $('.pager .nav').bind('mouseenter', function()
    {
       $el = this;
       
       $left = $($el).position().left - $('.pager .progress-bar').position().left - $('.pager .progress-bar .bar').width()/2 + 19;
      
       calculatePager($('.pager .progress-bar .bar'));
       
       $width = $('.pager .progress-bar .bar').width();
       $maxLeft = $('.pager .progress-bar').width();
     
       if ($left+$width <= $maxLeft) 
          $left = $left;
       else 
          $left = $maxLeft - $width+1;
      
       if ($left <= 0)
       {
           $left = 0;
       }
      
       $('.pager .progress-bar .bar').stop().animate({
           'left': $left 
       }); 
       
    }).bind('mouseleave', function() { $('.pager .current').each(function() { $(this).mouseenter(); }); });
    
    function calculateRank($progressbar)
    {
        $plus  = $progressbar.find('.orange-bar').attr('title');
        $minus = $progressbar.find('.gray-bar').attr('title');
        
        $all = parseInt($plus) + parseInt($minus);
        
        $fullWidth = $progressbar.width();
       
        if ($all!=0) { 
            $percentRank = parseInt(parseInt($plus*100)/$all);
            $plusWidth = parseInt(($fullWidth*$percentRank)/100);
            $minusWidth = parseInt($fullWidth-$plusWidth);
        }
        else
        {
            $plusWidth=0;
            $minusWidth=0;
        }

        $progressbar.find('.orange-bar').animate({
            'width' : $plusWidth-1
        });
        
        $progressbar.find('.gray-bar').animate({
            'width' : $minusWidth-1
        });           
    }
    
    $('.rank-component').each(function() {
        calculateRank($(this));
    });
    
    $('.pager .current').each(function() { $(this).mouseenter(); });
})
</script>